/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __UTL_H__
#define __UTL_H__

#undef min
#undef max

namespace liba
{
/** This namespace includes useful generic utilities, such as {@link #clamp} and {@link #sgn}
	@todo Move most methods to {@link liba::math} namespace. */
namespace utl
{

	template<typename T>
	class delete_ptr
	{
	public:
		T * operator() ( T * ptr )
		{
			delete ptr;
			return 0;
		}
	};

	template<class Objects, class It, class Compare>
	void eliminate_difference(Objects * objects, It begin, It end, Compare cmp)
	{
		typename Objects::iterator it1 = objects->begin();
		It it2 = begin;

		while( it1 != objects->end() && it2 != end )
			switch( cmp(*it1, *it2) )
			{
			case -1:
				it1 = objects->erase( it1 );
				break;
			case 1:
				it1 = objects->insert( it1, *it2 );
				++it2;
				break;
			default:
				++it1;
				++it2;
			}
		while( it1 != objects->end() )
		{
			it1 = objects->erase( it1 );
		}
		while( it2 != end )
		{
			it1 = objects->insert( it1, *it2 );
			++it2;
		}
	}


} // namespace utl
} //namespace liba
using namespace liba;

#endif //__UTL_H__